#!/bin/bash -eu

# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

if [ "$#" -ne 2 ] && [ "$#" -ne 3 ]; then
  echo "Illegal number of parameters, should one or two: the version, or the version and website-src directory";
  exit 1;
fi

URL_VERSION=$1
DRUID_VERSION=$2
WEBSRC="../../druid-website-src"

if [ "$#" -gt 2 ]; then
  WEBSRC=$3
fi

echo "Building to docs for '$URL_VERSION' to $WEBSRC ..."

npm run compile-scss

npm run build | tee ./build-log.txt

if grep -q "unresolved links in file" ./build-log.txt; then
  echo "There are unresolved links, please fix";
  rm build-log.txt;
  #exit 1
else
  rm build-log.txt
fi

node script/fix-path.js $URL_VERSION $DRUID_VERSION

echo "Cleaning..."
rm -rf $WEBSRC/docs/$URL_VERSION/
mkdir -p $WEBSRC/docs/$URL_VERSION/

cp -r ./build/ApacheDruid/docs/* $WEBSRC/docs/$URL_VERSION/

mkdir -p $WEBSRC/css
cp ./build/ApacheDruid/css/* $WEBSRC/css

mkdir -p $WEBSRC/js
cp ./build/ApacheDruid/js/* $WEBSRC/js
